package com.zhq.dao.imp;

import com.zhq.dao.BaseAdminDao;
import com.zhq.util.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

/**
 * @Author:钟豪强
 * @Description:
 * @Date Created in  2020-08-14 12:45
 * @Modified By:
 */
public class AdminDaoMysql implements BaseAdminDao {
    private static final String SQL_UPDATE_LOGIN_TIME = "UPDATE EADMIN SET LOGINTIME = ?, LOGINIP = ? WHERE USERNAME = ?";
    private static final String SQL_LOGIN = "SELECT ID FROM EADMIN WHERE USERNAME = ? AND PASSWORD = ?";
    private static final String SQL_FIND = "SELECT ID FROM EADMIN WHERE USERNAME = ?";

    /**
     * 根据用户名,更新登录时间和登录ip
     *
     * @param username 用户名(账户)
     * @param date     登录时间
     * @param ip       登录ip
     */
    @Override
    public void updateLoginTime(String username, Date date, String ip) {
        //1.获取连接
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        //2.预编译sql语句
        try {
            state = conn.prepareStatement(SQL_UPDATE_LOGIN_TIME);
            //3.填充参数
            state.setDate(1,new java.sql.Date(date.getTime()));
            state.setString(2,ip);
            state.setString(3,username);
            //4.执行
            state.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //5.释放资源
            DruidUtil.close(conn,state,null);
        }
    }

    /**
     * 管理员根据账号密码登录
     *
     * @param username 用户名(账户)
     * @param password 密码
     * @return 登录结果, true代表登录成功, false代表失败
     */
    @Override
    public boolean login(String username, String password) {
        //1.获取连接
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet rs = null;
        //2.预编译
        try {
            //3.填充参数
            state = conn.prepareStatement(SQL_LOGIN);
            state.setString(1,username);
            state.setString(2,password);
            //4.执行并获取结果
            rs = state.executeQuery();
            return rs.next();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtil.close(conn,state,rs);
        }
        return false;
    }

    /**
     * 管理员根据账号密码登录
     *
     * @param username 用户名(账户)
     * @return 登录结果, true代表登录成功, false代表失败
     */
    @Override
    public boolean find(String username) {
        //1.获取连接
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet rs = null;
        //2.预编译
        try {
            //3.填充参数
            state = conn.prepareStatement(SQL_FIND);
            state.setString(1,username);
            //4.执行并获取结果
            rs = state.executeQuery();
            return rs.next();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtil.close(conn,state,rs);
        }
        return false;
    }
}
